import { FormSchema } from '@/components/Table'
import { getClassifyDevice, getClassifyTree } from '@/api/collection/classify'
import dayjs from 'dayjs'
import { FormActionType } from '@/components/Form'

const format = 'YYYY-MM-DD HH:mm:ss'
export const searchFormSchema = (getForm: () => FormActionType) => {
  return [
    {
      field: 'treeId',
      label: '选择科室',
      component: 'ApiTreeSelect',
      colProps: { span: 4 },
      componentProps: ({ formModel, formActionType }) => {
        return {
          api: getClassifyTree,
          labelField: 'classifyName',
          valueField: 'treeId',
          popupClassName: 'form-search-tree-select',
          allowClear: true,
          onChange: async (value, label) => {
            const { list } = await getClassifyDevice({
              classifyId: value,
              pageSize: 100,
            })

            formModel.dataTypeId = undefined //  reset deviceId value
            const { updateSchema } = formActionType
            updateSchema({
              field: 'dataTypeId',
              componentProps: {
                options: list,
                labelInValue: true,
                disabled: !value,
                placeholder: '请先选择科室',

                fieldNames: {
                  label: 'classifyName',
                  value: 'dataTypeId',
                },
              },
            })
          },
        }
      },
    },
    {
      field: 'dataTypeId',
      label: '选择点位',
      component: 'Select',
      colProps: { span: 4 },
      componentProps: ({ formModel, formActionType }) => {
        return {
          disabled: !formModel.treeId,
          placeholder: '请先选择科室',
        }
      },
    },
    {
      field: 'measureTimeStart',
      labelWidth: 104,
      label: '开始时间',
      component: 'DatePicker',
      helpMessage: '起止时间差不能超过 24 小时',
      colProps: { span: 5 },
      defaultValue: dayjs().startOf('day'),
      componentProps: {
        valueFormat: format,
        format,
        showTime: true,
        onChange: () => {
          getForm().validateFields(['measureTimeEnd'])
        },
      },
      rules: [
        {
          validator: (rule, value, callback) => {
            const { measureTimeEnd } = getForm().getFieldsValue()
            const end = dayjs(measureTimeEnd)
            const start = dayjs(value)
            return new Promise((resolve, reject) => {
              if (measureTimeEnd) {
                if (start.isAfter(end)) {
                  reject(new Error('开始时间不得晚于结束时间'))
                } else if (end.diff(start, 'hour') > 24) {
                  reject(new Error('起止时间差不能超过 24 小时'))
                } else {
                  resolve()
                }
              } else {
                resolve()
              }
            })
          },
        },
      ],
    },
    {
      field: 'measureTimeEnd',
      label: '结束时间',
      component: 'DatePicker',
      colProps: { span: 5 },
      defaultValue: dayjs().endOf('day'),
      componentProps: {
        valueFormat: format,
        format,
        showTime: true,
        onChange: () => {
          getForm().validateFields(['measureTimeStart'])
        },
      },
      rules: [
        {
          validator: (rule, value, callback) => {
            const { measureTimeStart } = getForm().getFieldsValue()
            const start = dayjs(measureTimeStart)
            const end = dayjs(value)
            return new Promise((resolve, reject) => {
              if (measureTimeStart) {
                if (end.isBefore(start)) {
                  reject(new Error('结束时间不得早于开始时间'))
                } else if (end.diff(start, 'hour') > 24) {
                  reject(new Error('起止时间差不能超过 24 小时'))
                } else {
                  resolve()
                }
              } else {
                resolve()
              }
            })
          },
        },
      ],
    },
    {
      field: 'rangeButton',
      label: ' ',
      labelWidth: 8,
      slot: 'rangeButton',
    },
  ] as FormSchema[]
}

export const columns = [
  {
    title: '查询时间',
    dataIndex: 'queryTime',
  },
  {
    title: '点位名',
    dataIndex: 'dataTypeName',
  },
  {
    title: '当前数值',
    dataIndex: 'measureValue',
    width: 120,
    customRender: ({ text }) => (isNaN(text) ? '--' : text),
  },
  {
    title: '上一周期数值',
    dataIndex: 'upMeasureValue',
    width: 120,
    customRender: ({ text }) => (isNaN(text) ? '--' : text),
  },
  {
    title: '变化率',
    dataIndex: 'changeRate',
    width: 120,
    customRender: ({ text }) => (isNaN(text) ? '--' : `${(text * 100).toFixed(0)}%`),
  },
]

export const modalColumns = [
  {
    title: '标签',
    dataIndex: 'tag',
  },
  {
    title: '标签数据日期',
    dataIndex: 'createTime',
  },
  {
    title: '修改前值',
    dataIndex: 'oldData',
  },
  {
    title: '修改后值',
    dataIndex: 'newData',
  },
]
